<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    tr,td {
      border: 1px solid;
      padding: 10px 8px;
      background-color: #f6f6f6;
    }
    table {
      border-collapse: collapse;
    }
  </style>
</head>
<body>

  <div id="app">
    <table>
      <thead>
        <tr>
          <td>序号</td>
          <td>书籍名称</td>
          <td>出版日期</td>
          <td>价格</td>
          <td>购买数量</td>
          <td>操作</td>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item ,index) in books">
          <td>{{index+1}}</td>
          <td>{{item.name}}</td>
          <td>{{item.date}}</td>
          <td>{{item.price}}</td>
          <td>
            <button @click="decrease(index)">-</button>
            {{item.count}}
            <button @click="increase(item)">+</button>
          </td>
          <td><button @click="remove(index)">移除</button></td>
        </tr>
      </tbody>
    </table>
    <h2>总价:'￥' {{total}}</h2>
  </div>
  
  <script src="../lib/vue.js"></script>
  <script src="./data.js"></script>
  <script>
    // 1.创建app
    const app = Vue.createApp({
      // data: option api
      data: function() {
        return {
          books: books 
        }
      },
      methods: {
        decrease(index) {
          this.books[index].count--
        },
        increase(item) {
          item.count++
        },
        remove(index) {
          this.books.splice(index,1)
        }
      },
      computed: {
        total() {
          let preprice = 0
          for(const item of this.books) {
            preprice += item.price * item.count
          }
          return preprice
        }
      }
    })

    // 2.挂载app
    app.mount("#app")
  </script>
</body>
</html>